package jfox.platform.common;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

/**
 * @author <a href="mailto:jfox.young@gmail.com">Young Yang</a>
 */
public class Utils {

    public static final DateFormat longSimpleDateFormat = new SimpleDateFormat("yyyyMMdd");
    public static final DateFormat stringSimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

    public static final Utils instance = new Utils();

    public static Utils getInstance() {
        return instance;
    }

    private Utils() {
    }

    /**
     * 返回可读的Long型今天，如：20071201
     */
    public static long getTodayLong(){
        return Long.parseLong(longSimpleDateFormat.format(new Date()));
    }

    /**
     * 返回可读的String型今天，如：2007-12-01
     */
    public static String getTodayString(){
        return stringSimpleDateFormat.format(new Date());
    }

    public static String translateDate(long date){
        String _date = date + "";
        StringBuffer sb = new StringBuffer(10);
        sb.append(_date.substring(0,4)).append("-").append(_date.substring(4,6)).append("-").append(_date.substring(6,8));
        return sb.toString();
    }

    /**
     * 返回可读的Long型明天，如：20071202
     */
    public static long getTomorrowLong(){
        return Long.parseLong(longSimpleDateFormat.format(new Date(System.currentTimeMillis() + 24 * 60 * 60 * 1000)));
    }
    /**
     * 根据字符串时间转换为long 类型时间
     * @param date
     * @return
     */
    public static long getMyLongDate(String date){
    	String _year =date.substring(0,4);
    	String _month=date.substring(5,7);
    	String _day  =date.substring(8,10);
    	String _date=_year+_month+_day;
    	return Long.parseLong(_date);
    }

    /**
     * 将数组转化为字符串，以便保存
     * @param array
     * @return
     */
    public static String array2String(long[] array){
        if(array == null) {
            return "";
        }
        else {
            String str = Arrays.toString(array);
            return str.substring(1, str.length() -1);
        }
    }

    /**
     * 将字符串转换成数组，以便计算
     * @param str
     * @return
     */
    public static long[] string2Array(String str){
        if(str == null || str.trim().length() == 0) {
            return new long[0];
        }
        else {
            String[] sarray = str.split(",");
            long[] larray = new long[sarray.length];
            for(int i=0; i<sarray.length; i++){
                larray[i] = Long.parseLong(sarray[i].trim());
            }
            return larray;
        }
    }

    public static void main(String[] args) {
        System.out.println(translateDate(19991022));
    }
}
